package nmap

import (
	// "bytes"
	m "git.oschina.net/go-pkg/nmap/models"
	"os/exec"
)

const (
	DefaultNmapPath = "nmap"
)

type cmdNmap struct {
	Path string
}

func newCmdNmap() Nmap {
	inst := cmdNmap{
		Path: DefaultNmapPath,
	}
	return &inst
}

func (nmap *cmdNmap) scan(args ...string) (*m.NmapRun, error) {

	// var buf bytes.Buffer

	if bytes, err := exec.Command(nmap.Path, args...).Output(); nil != err {
		return nil, err
	} else {
		return nmap.parse(bytes)
	}
}

func (nmap *cmdNmap) parse(content []byte) (*m.NmapRun, error) {
	return m.ParseXml(content)
}

func (nmap *cmdNmap) Scan(targets []string, args ...string) (*m.NmapRun, error) {
	return nmap.scan(append(append(args, "-oX", "-"), targets...)...)
}

func (nmap *cmdNmap) PingScan(targets []string) (*m.NmapRun, error) {
	return nmap.scan(append(targets, "-sn", "-oX", "-")...)
}

func (nmap *cmdNmap) Scan2(args ...string) (*m.NmapRun, error) {
	return nmap.scan(append([]string{"-oX", "-"}, args...)...)
}

func (nmap *cmdNmap) Scan3(args []string, targets ...string) (*m.NmapRun, error) {
	return nmap.scan(append(args, targets...)...)
}

func (nmap *cmdNmap) PingScan2(targets ...string) (*m.NmapRun, error) {
	return nmap.PingScan(targets)
}
